package com.weibo.action;

import com.opensymphony.xwork2.ActionSupport;
import com.weibo.common.SessionManager;
import com.weibo.pojo.Tuser;
import com.weibo.service.IUserService;

public class LogRegAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private IUserService userService;
	private Tuser user;
	private String message="";
	private String comfirmPass;
	
	public String login() throws Exception {
		//user.setUserPass(new MD5().getMD5ofStr(user.getUserPass()));
		System.out.println(user.getUserPass());
		if(userService.isExistUser(user)){
			user = userService.getUserByName(user.getUserName());
			SessionManager.setSessionAttribute("nowUser", user);
			return SUCCESS;
		}
		else{
			message = "用户名或密码错误";
			return LOGIN;
		}
	}
	public String loginReady() throws Exception {
		
	/*	if(SessionManager.getAttribute("nowUser")!= null){
			System.out.println(((Tuser)SessionManager.getAttribute("nowUser")).getUserName());
			return "home";
		}*/
		return LOGIN;
	}
	
	public String register() throws Exception{
		if(user!=null){
			if(userService.isExistUser(user.getUserName())){
				message="用户名已经存在";
				return "register";
			}else{
				userService.saveUser(user);
				return LOGIN;
			}
		}else{
			message="注册失败";
			return "register";
		}
	}
	
	public String registerReady() throws Exception{
		return "register";
	}
	
	public IUserService getUserService() {
		return userService;
	}
	public void setUserService(IUserService userService) {
		this.userService = userService;
	}
	public Tuser getUser() {
		return user;
	}
	public void setUser(Tuser user) {
		this.user = user;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public String getComfirmPass() {
		return comfirmPass;
	}
	public void setComfirmPass(String comfirmPass) {
		this.comfirmPass = comfirmPass;
	}
	
}
